package cn.zcl.demo.controller;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.PrinterJob;
import java.io.File;

import static cn.zcl.demo.util.PrintUtil.getPrintServiceByName;
import static cn.zcl.demo.util.PrintUtil.setPageStyle;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: zhanchenlei
 * @Date: 2021/04/28/11:13
 * @Description:
 */
@RestController
@RequestMapping("a")
public class PrintController {
    /**
     * @Description: 调用打印机接口
     * @Param: [pdfPath：pdf文件路径]
     * @Param: [pdfPath：打印机名字]
     * @return: org.apache.pdfbox.pdmodel.PDDocument
     * @Author: zhanchenlei
     * @Date: 2021/4/28
     */
    @PostMapping("test")
    public static PDDocument printPdf(String pdfPath, String printerName) throws Exception {
        File file = new File(pdfPath);
        PDDocument document = PDDocument.load(file);
        //调用打印机
        PrinterJob job = getPrintServiceByName(printerName);
        //设置纸张样式
        setPageStyle(document, job);
        // 开始打印
        job.print();
        return document;
    }

}
